import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_he/page/mine/xin_he_mine_orders/xin_he_mine_order_detail/xin_he_new_order_detail_entity.dart';
import 'package:xin_he/utils/xin_he_UI_constancs.dart';

import '../../../../utils/xh_styles.dart';
import '../../../../utils/xin_he_image.dart';
import '../../../shopping_car/models/xin_he_goods_model.dart';
import 'xin_he_mine_order_detail_logic.dart';

class XinHeMineOrderDetailPage extends StatelessWidget {
  XinHeMineOrderDetailPage({Key? key}) : super(key: key);

  final logic = Get.put(XinHeMineOrderDetailLogic());
  final state = Get.find<XinHeMineOrderDetailLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Obx(() => Scaffold(
      appBar: AppBar(
        leading: IconButton(
            onPressed: (){
              Get.back();
            },
            icon: Container(
              padding: const EdgeInsets.only(left: 7),
              alignment: Alignment.centerLeft,
              child: const XinHeAssetImage(
                'assets/images/home/app_back_3.png',
                width: 9.5,
                height: 17,
                fit: BoxFit.cover,),
            )
        ),
        backgroundColor: Colors.white,
        shadowColor: Colors.transparent,
        title: Text('订单详情',style: TextStyle(color:XinHeColor('##222222'),fontSize: 17,fontWeight: FontWeight.w600)),
        centerTitle: true,
      ),
      body: logic.model.value.id != null? Column(children: [
        Expanded(child: ListView(
          children: [
            ///订单状态
            Container(
              height:69*2,
              decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.circular(7)),
              margin: const EdgeInsets.only(left: 15,right: 15,top: 15,bottom: 10),
              padding: const EdgeInsets.symmetric(horizontal: 15),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [



                  if(logic.model.value.status! == 'UN_PAY') ...[
                    SizedBox(
                      height: 69,
                      child: Row(children: [
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text('等待买家付款',style: TextStyle(color:XinHeStyle.mainBalckFontColor,fontSize: 15,fontWeight: FontWeight.w500 ),),
                            Text('超时未付款，订单将自动取消！！！',style: TextStyle(color:XinHeColor('#555555'),fontSize: 13,fontWeight: FontWeight.w500 ),),

                          ],),
                        Spacer(),
                        // Container(
                        //   height: 21,
                        //   width: 65,
                        //   alignment: Alignment.center,
                        //   decoration: BoxDecoration(color: XinHeColor('#E5E5E5'),borderRadius: BorderRadius.circular(10.5)),
                        //   child: Text(''),)
                      ],),

                    ),
                  ],
                  if(logic.model.value.status! == 'PAYED') ...[
                    SizedBox(
                      height: 69,
                      child: Row(children: [
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text('等待卖家发货',style: TextStyle(color:XinHeStyle.mainBalckFontColor,fontSize: 15,fontWeight: FontWeight.w500 ),),
                            Text('买家已付款，请商家尽快发货！',style: TextStyle(color:XinHeColor('#555555'),fontSize: 13,fontWeight: FontWeight.w500 ),),

                          ],),
                        Spacer(),
                        // Container(
                        //   height: 21,
                        //   width: 65,
                        //   alignment: Alignment.center,
                        //   decoration: BoxDecoration(color: XinHeColor('#E5E5E5'),borderRadius: BorderRadius.circular(10.5)),
                        //   child: Text(''),)
                      ],),

                    ),

                  ],
                  if(logic.model.value.status! == 'SENDED') ...[
                    SizedBox(
                      height: 69,
                      child: Row(children: [
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text('卖家已发货',style: TextStyle(color:XinHeStyle.mainBalckFontColor,fontSize: 15,fontWeight: FontWeight.w500 ),),
                            Text('待买家收货！',style: TextStyle(color:XinHeColor('#555555'),fontSize: 13,fontWeight: FontWeight.w500 ),),

                          ],),
                        Spacer(),
                        // Container(
                        //   height: 21,
                        //   width: 65,
                        //   alignment: Alignment.center,
                        //   decoration: BoxDecoration(color: XinHeColor('#E5E5E5'),borderRadius: BorderRadius.circular(10.5)),
                        //   child: Text(''),)
                      ],),

                    ),

                  ],
                  if(logic.model.value.status! == 'RECEIVED') ...[
                    Container(
                      alignment: Alignment.centerLeft,
                      height: 69,
                      child: Row(children: [
                        const XinHeAssetImage('assets/images/mine/success_s_25.png',width: 25,height: 25,),
                        const SizedBox(width: 10,),
                        Text('交易完成',style: TextStyle(color: XinHeStyle.mainBalckFontColor,fontWeight: FontWeight.w500,fontSize: 15),)
                      ],),),

                  ],

                  Container(height: 0.5,color: XinHeStyle.lineColor,width: Get.width-30,),

                  SizedBox(
                    height: 69-0.5,
                    width: Get.width-60,
                    child: Row(children: [
                      const XinHeAssetImage('assets/images/cart/pay_location.png',width: 15,height: 19,),
                      const SizedBox(width: 5,),
                      SizedBox(
                        width: Get.width-60 - 15-5-6-10,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            SizedBox(height: 5,),
                            // Text('北京东门店',style: TextStyle(color: XinHeColor('#222222'),fontSize: 13,fontWeight: FontWeight.w600),),
                            Text('${logic.model.value.orderAddress}',
                              maxLines: 2,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(color: XinHeColor('#777777'),fontSize: 12,fontWeight: FontWeight.w600),)
                          ],),
                      ),
                      SizedBox(width: 10,),
                      // const XinHeAssetImage('assets/images/cart/pay_arrow.png',width: 6,height: 10,),

                    ],),
                  )


                ],),
            ),

            ///商品信息
            Container(
              margin: const EdgeInsets.symmetric(horizontal: 15).copyWith(bottom: 10),
              padding: const EdgeInsets.symmetric(horizontal: 15),
              decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.circular(7)),

              child: Column(
                children: [
                  SizedBox(
                    height: 44,
                    child: Row(children: [
                      const XinHeAssetImage('assets/images/cart/cart_unselected.png',width: 18,height: 18,),
                      const SizedBox(width: 5,),
                      Text(XinHeShopInfo.name,style: TextStyle(color: XinHeColor('#282C2C'),fontSize: 13,fontWeight: FontWeight.w500),),
                      const SizedBox(width: 3,),
                      const XinHeAssetImage('assets/images/cart/pay_arrow.png',width: 6,height: 10,),
                      Spacer(),


                    ],),
                  ),

                  // _goodsItemView(XinHeGoodsModel(),0),

                  ListView.builder(
                      physics: const NeverScrollableScrollPhysics(),
                      shrinkWrap: true,
                      itemCount: logic.model.value.orderItems?.length,
                      itemBuilder: (ctx,ind){
                        XinHeNewOrderDetailOrderItems item = logic.model.value.orderItems![ind];
                        return _goodsItemView(item, ind, 0);
                      }),

                  SizedBox(height: 5,)


                ],),),

            ///总金额
            Container(
              margin: const EdgeInsets.symmetric(horizontal: 15),
              height: 210,
              decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.circular(10)),
              child: Column(children: [
                totalAmountItemWidget('总金额','${logic.model.value.orderFee}'),
                // totalAmountItemWidget('消费金红包','暂无可用'),
                Container(
                  height: 42,
                  alignment: Alignment.center,
                  padding: const EdgeInsets.symmetric(horizontal: 15),
                  child: Row(children: [
                    const XinHeAssetImage('assets/images/cart/pay_method_redbag.png',width:12,height:14),
                    SizedBox(width: 5,),
                    Text('消费金红包',style: TextStyle(color: XinHeColor('#222222'),fontSize: 13),),
                    Spacer(),
                    Text('暂无可用',style: TextStyle(color: XinHeColor('#B0B0B0'),fontSize: 15),),

                  ],),
                ),

                totalAmountItemWidget('运费','0.00'),
                totalAmountItemWidget('实际支付','${logic.model.value.orderFee}'),
                Container(
                  height: 42,
                  alignment: Alignment.center,
                  padding: const EdgeInsets.symmetric(horizontal: 15),
                  child: Row(children: [
                    Text('订单备注',style: TextStyle(color: XinHeColor('#222222'),fontSize: 13),),
                    Spacer(),
                    Container(
                      width: 130,
                      height: 42,
                      alignment: Alignment.centerRight,
                      child: Text(logic.model.value.buyerRemark!.isNotEmpty?logic.model.value.buyerRemark!:'无', style: TextStyle(
                          color: XinHeColor("#15161F"), fontSize: 14 ,fontWeight: FontWeight.w400)),
                    ),


                  ],),
                )


              ],),
            ) ,

            ///订单编号
            Container(
              margin: const EdgeInsets.symmetric(horizontal: 15,vertical: 10),
              padding: const EdgeInsets.symmetric(horizontal: 15,vertical: 6),
              // height: 87,
              decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.circular(10)),
              child: Column(
                children: [
                  Container(
                    height: 25,
                    alignment: Alignment.centerLeft,
                    child: Row(children: [
                      Text('订单编号',style: TextStyle(color: XinHeColor('#222222'),fontSize: 12,fontWeight: FontWeight.w500),),
                      Spacer(),
                      Text('${logic.model.value.id}',style: TextStyle(color: XinHeColor('#666666'),fontSize: 12,fontWeight: FontWeight.w500),),

                    ],),
                  ),

                  Container(
                    height: 25,
                    alignment: Alignment.centerLeft,
                    child: Row(children: [
                      Text('创建时间',style: TextStyle(color: XinHeColor('#222222'),fontSize: 12,fontWeight: FontWeight.w500),),
                      Spacer(),
                      Text('${logic.model.value.createTime}',style: TextStyle(color: XinHeColor('#666666'),fontSize: 12,fontWeight: FontWeight.w500),),

                    ],),
                  ),

                logic.model.value.status != 'UN_PAY' ? Container(
                    height: 25,
                    alignment: Alignment.centerLeft,
                    child: Row(children: [
                      Text('支付时间',style: TextStyle(color: XinHeColor('#222222'),fontSize: 12,fontWeight: FontWeight.w500),),
                      Spacer(),
                      Text('${logic.model.value.payTime}',style: TextStyle(color: XinHeColor('#666666'),fontSize: 12,fontWeight: FontWeight.w500),),

                    ],),
                  ):Container()

                ],),),




          ],)
        ),



        /// 底部按钮
        Container(
            height: 60,
            alignment: Alignment.centerRight,
            padding: const EdgeInsets.only(right: 15),
            decoration: BoxDecoration(color: Colors.white),
            child: Row(children: [
              Spacer(),
              Container(
                height: 27,
                width: 71,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(13.5),
                    border: Border.all(color: XinHeColor('#B9B9B9'),width: 0.5)
                ),
                child: Text(logic.getLeftButtonTitle(logic.model.value.status!),style: TextStyle(color: XinHeColor('#666666'),fontSize: 12),strutStyle: const StrutStyle(forceStrutHeight: true)),
              ),
              const SizedBox(width: 10,),
              GestureDetector(
                onTap: () {
                  logic.rightButtionAction();
                },
                child: Container(
                  height: 27,
                  width: 71,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    gradient: LinearGradient(colors: [ XinHeColor('##F89A30'), XinHeColor('#E84128')]),
                    borderRadius: BorderRadius.circular(13.5),
                  ),
                  child:  Text(logic.getRightButtonTitle(logic.model.value.status!),style: TextStyle(color: Colors.white,fontSize: 12),strutStyle: StrutStyle(forceStrutHeight: true),),
                ),
              ),

            ],)
        ),
        SizedBox(height: Get.mediaQuery.padding.bottom,),
      ],):const Center(child: CupertinoActivityIndicator(),),
    ));
  }


  Widget _goodsItemView(XinHeNewOrderDetailOrderItems model,int index,int section){
    return Stack(
      children: [
        Container(
          color: Colors.white,
          width: Get.width-30,
          height: 90,

          child:  Row(
            children: [

               Padding(
                padding:  const EdgeInsets.only(left: 0,top: 0,right: 15,bottom: 10),
                child: XinHeNetImage(url: model.iconPath??'',width: 80,height: 80,fit: BoxFit.fill),
              ),

              ///描述与价格数量
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(
                      width:Get.width-30-80-30-15,
                      child: Text('${model.goodsName}',maxLines: 2,softWrap: true,style: TextStyle(color: XinHeColor('#222222'),fontSize: 14,fontWeight: FontWeight.bold),)
                  ),
                  const SizedBox(height: 10,),



                  SizedBox(
                    width:Get.width-30-80-30-15,
                    height: 23,
                    child: Row(
                      children: [

                        RichText(
                            text: TextSpan(children: [
                              ///￥
                              TextSpan(
                                text: '￥',
                                style: TextStyle(
                                  color: XinHeColor('#E84128'),
                                  fontSize: 9,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),

                              ///
                              TextSpan(
                                text: '${model.itemPrice}',
                                style: TextStyle(
                                  color: XinHeColor('#E84128'),
                                  fontSize: 16,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),

                            ]
                            )),
                        const Spacer(),

                        Text('x${model.goodsAmount}',style: TextStyle(color: XinHeColor('#8B9191'), fontSize: 12, fontWeight: FontWeight.w500,)
                        ),
                        const SizedBox(width: 0,)

                      ],
                    ),
                  )

                ],)


            ],
          ),
        ),

        // Positioned(
        //   bottom: 0,
        //   right: 15,
        //   child: Container(
        //     width: Get.width-30-30,
        //     color: XinHeColor('#E7EEEE'),
        //     height: 0.5,),
        // )

      ],
    );
  }


  Widget  totalAmountItemWidget(String title,String titleValue){
    return Container(
      height: 42,
      alignment: Alignment.center,
      padding: const EdgeInsets.symmetric(horizontal: 15),
      child: Row(children: [
        Text(title,style: TextStyle(color: XinHeColor('#222222'),fontSize: 13),),
        Spacer(),
        Text(titleValue,style: TextStyle(color: XinHeColor('#222222'),fontSize: 15),),

      ],),
    );
  }

}
